package cn.itsource.pet.mapper;

import cn.hutool.core.util.StrUtil;
import cn.itsource.basic.mysuper.MyBaseMapper;

import cn.itsource.pet.domain.SearchMasterMessage;
import cn.itsource.pet.domain.SearchMasterMessageQuery;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

public interface SearchMasterMessageMapper extends MyBaseMapper<SearchMasterMessage> {
    @Override
   default Class<SearchMasterMessage> getTClass(){
        return SearchMasterMessage.class;
    };

    default List<SearchMasterMessage> pagelist(SearchMasterMessageQuery query,Long shopId){
        //1.创建分页对象
//        PageHelper.startPage(query.getPage(),query.getPageSize());
        Example example = new Example(getTClass());
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria criteria1 = example.createCriteria();

        criteria.andEqualTo("shopId", shopId)
                .orCondition("shop_id is null");
        System.out.println(query.getKeyword());
        //如果关键字不为空
        if (StrUtil.isNotBlank(query.getKeyword())){
            criteria1.andLike("title","%"+ query.getKeyword()+"%")
                    .orLike("address","%"+ query.getKeyword()+"%");
        }
        //如果状态不为空
        if (Objects.nonNull(query.getState())){
            criteria1.
                    andEqualTo("state",query.getState());
        }
        //如果时间不为空
        if (StrUtil.isNotBlank(query.getBetweenTime())){
            String[] split = query.getBetweenTime().split(",");
            criteria1.
                    andBetween("createTime",split[0],split[1]);
        }
        //第二步:查所有
        example.and(criteria1);
        List<SearchMasterMessage> searchMasterMessages = this.selectByExample(example);
        //3.用userinfo封装
//        PageInfo<SearchMasterMessage> pageInfo = new PageInfo<>(searchMasterMessages);
        return searchMasterMessages;
    };
}
